package com.example.lib_patrol

import androidx.lifecycle.viewModelScope
import com.example.lib_common.mvi.BaseViewModel
import com.example.lib_common.mvi.ResponseEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class PatrolViewModel : BaseViewModel<PatrolRepository,PatrolState>() {
    override fun createRepo(): PatrolRepository {
        return PatrolRepository()
    }

    override fun createState(): PatrolState {
        return PatrolState(ResponseEntity(0,"", arrayListOf(PatrolEntity())))
    }
    fun requestAll(){
        mutableContainer.updateRequestState { copy(
            request = true
        ) }
        viewModelScope.launch(Dispatchers.IO) {
            repo.requestAllRepair().flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity=it
                    ) }
                }
        }
    }
}